package com.green_reading.mandolin;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.BindView;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SignupActivity extends Activity {
    private static final String TAG = SignupActivity.class.getCanonicalName();;

    @BindView(R.id.user_name) EditText user_name;
    @BindView(R.id.user_password) EditText user_password;
    @BindView(R.id.user_password_repeat) EditText user_password_repeat;
    @BindView(R.id.user_nickname) EditText user_nickname;
    @BindView(R.id.user_phone) EditText user_phone;
    @BindView(R.id.button_signup) Button button_signup;

    protected void toast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        ButterKnife.bind(this);

        button_signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name_str = user_name.getText().toString();
                String password_str = user_password.getText().toString();
                String user_password_repeat_str = user_password_repeat.getText().toString();
                String user_nickname_str = user_nickname.getText().toString();
                String user_phone_str = user_phone.getText().toString();
                Log.d(TAG, name_str + " " + password_str + " " +
                        user_nickname_str + " " + user_phone_str);
                if (!validate(name_str, password_str, user_password_repeat_str,
                        user_nickname_str, user_phone_str)) {
                    return;
                }
                button_signup.setEnabled(false);

                singup(name_str, Util.md5(password_str), user_nickname_str, user_phone_str);
            }
        });
    }

    private int singup(String user_name, String password, String nickname, String phone) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(SimpleService.BASE_URL)
                .build();

        SimpleService.ServerAPI service = retrofit.create(SimpleService.ServerAPI.class);
        Call<ResponseBody> call = service.register_user(user_name, password, nickname, phone);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {
                JSONObject responseJson = Util.parseResponse(response, TAG);
                if (response.code() == 200) {
                    toast("register success, you can login now!");
                    finish();
                } else {
                    toast(responseJson.optString("detail"));
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        button_signup.setEnabled(true);
                    }
                });
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                // Log error here since request failed
                toast("cannot connect to server");
                t.printStackTrace();
            }
        });
        return 0;
    }

    private boolean validate(String name_str, String password_str, String user_password_repeat_str,
                             String user_nickname_str, String user_phone_str) {

        if (name_str.equals("") || name_str.length() > 32) {
            toast("the length of user name should between 0 and 32");
            return false;
        }
        if (password_str.equals("") || password_str.length() > 32) {
            toast("the length of password should between 0 and 32");
            return false;
        }
        if (user_nickname_str.length() > 32) {
            toast("nickname too long!");
            return false;
        }
        if (user_phone_str.length() > 32) {
            toast("Email or phone number too long!");
            return false;
        }
        if (!password_str.equals(user_password_repeat_str)) {
            toast("password not same!");
            return false;
        }
        return true;
    }
}
